<?php

namespace app\api\controller;

use app\common\model\AccountRecordModel;
use app\common\model\EquipmentModel;
use app\common\model\EquipmentServiceOrdersModel;
use app\common\model\CompanyModel;
use app\common\model\AccountModel;
use think\App;

/**  YSF
 *   账户管理 API
 *   Date: 2019/1/29
 * Class Account
 * @package app\api\controller
 */
class Account extends Base
{
    protected $equipmentServiceOrdersModel;
    protected $equipmentModel;
    protected $accountRecordModel;
    protected $companyModel;
    protected $accountModel;

    // 初始化构造方法
    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->equipmentServiceOrdersModel = new EquipmentServiceOrdersModel(); // 设备服务费订单表模型
        $this->equipmentModel = new EquipmentModel(); // 设备表模型
        $this->accountRecordModel = new AccountRecordModel();  // 账户记录表模型
        $this->companyModel=new CompanyModel();
        $this->accountModel=new AccountModel();//账户模型
    }

    /**
     *   crm账户管理设备统计接口
     * @return \think\response\Json
     */
    public function getEquipmentCount()
    {
        // 接收参数
        $company_id = $this->parm['company_id'] ?? '';
        $o_sn = $this->parm['o_sn'] ?? '';
        $a_sn = $this->parm['a_sn'] ?? '';

        if (empty($company_id)) {
            returnJson(0, '缺少参数');
        }
        $account_info=$this->accountModel->getCompanyInfo($company_id,$o_sn,$a_sn);
        if(!empty($account_info) && !empty($account_info['account_id'])){
            $where[] = ['account_id', '=', $account_info['account_id']];
        }else{
            return json(['code' => 0, 'message' => '暂无账户信息','data'=>'']);
        }
        // 本月新增设备台数
        $firstday = date('Y-m-01 00:00:00', strtotime(date("Y-m-d")));  // 本月开始时间
        $lastday = date('Y-m-d 23:59:59', strtotime("$firstday +1 month -1 day")); // 本月结束时间

        // 4-- 本月可开发票
        $where[] = ['create_time', 'BETWEEN', [strtotime($firstday), strtotime($lastday)]];

        $data['invoice_count'] = $this->equipmentServiceOrdersModel->getSum($where, [],'money');
        if ($data['invoice_count']) {
            $data['invoice_count'] = formatMoney($data['invoice_count']);
        }
        // 1-- 本月新装统计
        $where[] = ['type', '=', 1];
        $data['add_count'] = $this->equipmentServiceOrdersModel->getCount($where);

        // 2-- 本月续约统计
        unset($where);
        if(!empty($account_info)){
            $where[] = ['account_id', '=', $account_info['account_id']];
        }
        $where[] = ['type', '=', 2];
        $where[] = ['create_time', 'BETWEEN', [strtotime($firstday), strtotime($lastday)]];
        $data['renew_count'] = $this->equipmentServiceOrdersModel->getCount($where);
        unset($where);

        // 3-- 本月到期统计
        $where[] = ['service_expire_time', 'BETWEEN', [strtotime($firstday), strtotime($lastday)]];
        if(!empty($account_info)){
            $where[] = ['company_no', '=', $account_info['company_no']];
        }
        $data['expire_count'] = $this->equipmentModel->getCount($where);
        unset($where);

        if ($data) {
            return json(['code' => 1, 'message' => '获取成功', 'data' => $data]);
        }
        return json(['code' => 0, 'message' => '暂无数据', 'data' => $data]);

    }

    /**
     *   新装和续约设备记录列表
     * @return \think\response\Json
     */
    public function getAddEquipmentList()
    {
        // 接收参数
        $page = $this->parm['page'] ?? 1;
        $pageSize = $this->parm['pageSize'] ?? 10;
        $o_sn = $this->parm['o_sn'] ?? '';
        $a_sn = $this->parm['a_sn'] ?? '';

        if (!empty($this->parm['equipment_id'])) {//1新装2续约
            $where[] = ['equipment_id', 'IN', $this->parm['equipment_id']];
        }
        if (!empty($this->parm['type'])) {
            $where[] = ['type', 'IN', $this->parm['type']];
        }

        $account_info=$this->accountModel->getCompanyInfo($this->parm['company_id'],$o_sn,$a_sn);
        if(!empty($account_info) && !empty($account_info['account_id'])){
            $where[] = ['account_id', '=', $account_info['account_id']];
        }else{
            returnJson(0, '暂无账户信息');
        }
        // 判断用户有没有输开始日期 // 判断用户有没有输结束日期
        if (!empty($this->parm['startTime']) && !empty($this->parm['endTime'])) {
            $where[] = ['create_time', 'BETWEEN', [strtotime($this->parm['startTime']), strtotime($this->parm['endTime']) + (60 * 60 * 24 - 1)]];
        } else if (!empty($this->parm['startTime'])) {
            $where[] = ['create_time', '>=', strtotime($this->parm['startTime'])];
        } else if (!empty($this->parm['endTime'])) {
            $where[] = ['create_time', '<=', strtotime($this->parm['endTime']) + (60 * 60 * 24 - 1)];
        }else{
            // 本月新装设备台数
            $firstday = date('Y-m-01 00:00:00', strtotime(date("Y-m-d")));  // 本月开始时间
            $lastday = date('Y-m-d 23:59:59', strtotime("$firstday +1 month -1 day")); // 本月结束时间

            $where[] = ['create_time', 'BETWEEN', [strtotime($firstday), strtotime($lastday)]];
        }

        // 列表查询
        $data = $this->equipmentServiceOrdersModel->getAll($where, 'order_sn,equipment_id,account_id,type,money,service_start_time,service_end_time,create_time', $page, $pageSize,[],'create_time desc');

        if (empty($data)) {
            return json(['code' => -1000, 'message' => '暂无数据', 'data' => '']);
        }

        // 数量查询
        $count = $this->equipmentServiceOrdersModel->getCount($where);
        $allPage = ceil($count / $pageSize);
        $arr['data'] = $data;
        $arr['count'] = $count;
        $arr['allPage'] = $allPage;

        return json(['code' => 1000, 'message' => '获取成功', 'data' => $arr]);
    }

    /**
     *   到期设备记录列表
     * @return \think\response\Json
     */
    public function getEquipmentExpireList()
    {
        // 接收参数
        $page = $this->parm['page'] ?? 1;
        $pageSize = $this->parm['pageSize'] ?? 10;
        $o_sn = $this->parm['o_sn'] ?? '';
        $a_sn = $this->parm['a_sn'] ?? '';

        if (!empty($this->parm['equipment_id'])) {
            $where[] = ['equipment_id', 'IN', $this->parm['equipment_id']];
        }
        $account_info=$this->accountModel->getCompanyInfo($this->parm['company_id'],$o_sn,$a_sn);
        if(!empty($account_info) && !empty($account_info['account_id'])){
            $where[] = ['account_id', '=', $account_info['account_id']];
        }else{
            returnJson(0, '暂无账户信息');
        }
        // 判断用户有没有输开始日期 // 判断用户有没有输结束日期
        if (!empty($this->parm['startTime']) && !empty($this->parm['endTime'])) {
            $where[] = ['create_time', 'BETWEEN', [strtotime($this->parm['startTime']), strtotime($this->parm['endTime']) + (60 * 60 * 24 - 1)]];
        } else if (!empty($this->parm['startTime'])) {
            $where[] = ['create_time', '>=', strtotime($this->parm['startTime'])];
        } else if (!empty($this->parm['endTime'])) {
            $where[] = ['create_time', '<=', strtotime($this->parm['endTime']) + (60 * 60 * 24 - 1)];
        }else{
            // 本月设备到期
            $firstday = date('Y-m-01 00:00:00', strtotime(date("Y-m-d")));  // 本月开始时间
            $lastday = date('Y-m-d 23:59:59', strtotime("$firstday +1 month -1 day")); // 本月结束时间

            $where[] = ['service_expire_time', 'BETWEEN', [strtotime($firstday), strtotime($lastday)]];
        }

        // 列表查询
        $data = $this->equipmentModel->getAll($where, 'equipment_id,company_no,app_sn,service_start_time,service_expire_time,last_pay_service_time,create_time', $page, $pageSize);
        if (empty($data)) {
            return json(['code' => -1000, 'message' => '暂无数据', 'data' => '']);
        }
        // 数量查询
        $count = $this->equipmentModel->getCount($where);
        $allPage = ceil($count / $pageSize);
        $arr['data'] = $data;
        $arr['count'] = $count;
        $arr['allPage'] = $allPage;

        return json(['code' => 1000, 'message' => '获取成功~', 'data' => $arr]);
    }

    // 可开发票记录列表
    public function getInvoiceMaganeList()
    {

    }

    /**
     *   获取账户消费记录
     */
    public function getConsumption()
    {
        // 接收参数
        $page = $this->parm['page'] ?? 1;
        $pageSize = $this->parm['pageSize'] ?? 10;
        $o_sn = $this->parm['o_sn'] ?? '';
        $a_sn = $this->parm['a_sn'] ?? '';

        $where[] = ['a.type', '=', 2]; // 2减少
        $where[] = ['a.source', 'IN', [3,4,5]]; //3设备新装服务费 4设备续费服务费 5服务费退还

        $account_info=$this->accountModel->getCompanyInfo($this->parm['company_id'],$o_sn,$a_sn);
        if(!empty($account_info) && !empty($account_info['account_id'])){
            $where[] = ['a.account_id', '=', $account_info['account_id']];
        }else{
            returnJson(0, '暂无账户信息');
        }

        // 判断用户有没有输开始日期 // 判断用户有没有输结束日期
        if (!empty($this->parm['startTime']) && !empty($this->parm['endTime'])) {
            $where[] = ['a.create_time', 'BETWEEN', [strtotime($this->parm['startTime']), strtotime($this->parm['endTime']) + (60 * 60 * 24 - 1)]];
        } else if (!empty($this->parm['startTime'])) {
            $where[] = ['a.create_time', '>=', strtotime($this->parm['startTime'])];
        } else if (!empty($this->parm['endTime'])) {
            $where[] = ['a.create_time', '<=', strtotime($this->parm['endTime']) + (60 * 60 * 24 - 1)];
        }
        $join=[
            ['equipment_service_orders o','o.id=a.order_id','LEFT']
        ];

        $field = 'a.*,o.order_sn,o.equipment_id';
        $order = 'a.create_time DESC';
        // 列表查询
        $data = $this->accountRecordModel->getAll($where, $field, $page, $pageSize, $join,$order);
        // 数量查询
        $count = $this->accountRecordModel->getCount($where,$join);
        // 计算总页数
        $page_sum = ceil($count / $pageSize);
        $info = [
            'page_current' => $page,
            'limit' => $pageSize,
            'page_sum' => $page_sum,
        ];
        if ($data) {
            foreach ($data as $k => &$v) {
                $v['money'] = formatMoney($v['money']);
                $v['type_name'] = config('api.account_type')[$v['type']];
                $v['source_name'] = config('api.account_source')[$v['source']];
                $v['create_time'] = strlen($v['create_time']) > 5 ? dateTime($v['create_time']) : '';
            }
            returnJson(1, '获取成功', $data, $count, $info);
        }
        returnJson(0, '暂无订单');
    }


}